//
//  GXManagerTimeController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/28.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXManagerTimeController.h"

@interface GXManagerTimeController ()
// 标签字典
@property (nonatomic,strong) NSDictionary *markDict;
// 选中标签数组(数字)
@property (nonatomic,strong) NSMutableArray *selectedMarkArray;
@property (nonatomic,strong) NSArray *timeArray;
@property (nonatomic,copy)NSString *baseTimeStr;
@property (nonatomic,copy)NSString *changeTimeStr;
@end

@implementation GXManagerTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"经营周期";
    [self setUpWeekTimeValue];
}
- (void)setUpWeekTimeValue
{
    self.timeArray = [NSArray array];
    if ([NSString isBlankString:self.dayStr]) {
        return;
    }
    self.baseTimeStr = self.dayStr;
    if (![NSString isBlankString:self.baseTimeStr]) {
        self.timeArray = [self.baseTimeStr  componentsSeparatedByString:@"-"];
        for (int i =0; i<self.timeArray .count; i++) {
            NSInteger j = [self.timeArray [i] integerValue] +90;
            UIButton * timeButton = [self.view viewWithTag:j];
            timeButton.selected = YES;
        }
        [self.selectedMarkArray addObjectsFromArray:self.timeArray];
    }
}

- (NSMutableArray *)selectedMarkArray {
    if (!_selectedMarkArray) {
        _selectedMarkArray = [NSMutableArray array];
    }
    return _selectedMarkArray;
}
- (IBAction)selectDayBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSString *buttonStr = [NSString stringWithFormat:@"%ld",sender.tag-90];
    if (sender.selected) {
        [self.selectedMarkArray addObject:buttonStr];
    }else
    {
        [self.selectedMarkArray removeObject:buttonStr];
    }
}
- (IBAction)pressSaveBtn:(id)sender {
    if (!self.selectedMarkArray.count) {
        [SVProgressHUD showErrorWithStatus:@"请选择经营时间"];
        return;
    }
    if (![self judgeChangeManagerTime]) {
        [SVProgressHUD showErrorWithStatus:@"选择经营时间未改变"];
        return;
    }
    [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    if (self.daySelectBlock) {
        self.daySelectBlock(self.changeTimeStr);
    }
    
}
- (BOOL)judgeChangeManagerTime
{
    NSArray *result = [self.selectedMarkArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];//升序处理
    }];
    self.changeTimeStr = [result componentsJoinedByString:@"-"];
    if ([self.changeTimeStr isEqualToString:self.baseTimeStr]) {
        return NO;
    }
    return YES;
}
    
    


@end
